﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace BasePlatformCommon
{
	[JsonObject(MemberSerialization.OptIn)]
	public class MainPort : Device
	{
		public bool HideFlag { get;	set; }					// 界面隐藏标记

		[JsonProperty]
		public int TotalSubControlChainNum { get; set; }	// 总分控链路数

		[JsonProperty]
		public int DeclareSubControlNum { get; set; }		// 声明的分控数

		[JsonProperty]
		public int TotalSubControlNum { get; set; }			// 总分控数

		[JsonProperty]
		public int ErrorSubControlNum { get; set; }			// 异常分控数

		[JsonProperty]
		public int TotalLampNum { get; set; }				// 总灯点数

		[JsonProperty]
		public int ErrorLampNum { get; set; }				// 异常灯点数

		[JsonProperty]
		public List<SubControlChain> SubControlChainList { get; set; }	// 分控链路数据集合

		// 构造函数
		public MainPort()
		{
			deviceType = DeviceType.MainPort;

			this.name = "端口";
			if (LanguageOper.available == true)
			{
				this.name = LanguageOper.ReadValue("Chain", "mainPort.name");
			}

			HideFlag = false;
			DeclareSubControlNum = -1;
			TotalSubControlChainNum = 0;
			ErrorSubControlNum = 0;
			ErrorLampNum = 0;
			TotalLampNum = 0;
			SubControlChainList = new List<SubControlChain>();
		}

		#region Json交互相关操作

		// 设置主控端口数据
		public void setMainPortData(JObject jsonData)
		{
			parseSubControlChainListData(jsonData);
			
			parseMainPortState();
		}

		// 解析分控链路集合数据
		private void parseSubControlChainListData(JObject jsonData)
		{
			try
			{
				TotalSubControlChainNum = (int)jsonData["TotalSubControlChainNum"];
				for (int chainIndex = 0; chainIndex < TotalSubControlChainNum; chainIndex++)
				{
					string chainNameStr = "SubControlChain" + chainIndex.ToString();
					parseSubControlChainData((JObject)jsonData[chainNameStr]);
				}

				DeclareSubControlNum = (int)jsonData["DeclareSubControlNum"];
			}
			catch
			{
				Log.DebugLogger.Error("SubControlChainList Parse Error!");
			}
		}

		// 解析分控链路数据
		private void parseSubControlChainData(JObject jsonData)
		{
			SubControlChain subControlChain = new SubControlChain();
			subControlChain.setSubControlChainData(jsonData);
			SubControlChainList.Add(subControlChain);
		}
		
		// 解析主控端口状态
		private void parseMainPortState()
		{
			for (int index = 0; index < TotalSubControlChainNum; index++)
			{
				TotalSubControlNum += SubControlChainList[index].TotalSubControlNum;
				TotalLampNum += SubControlChainList[index].TotalLampNum;
				ErrorSubControlNum += SubControlChainList[index].ErrorSubControlNum;
				ErrorLampNum += SubControlChainList[index].ErrorLampNum;
			}
		}

		#endregion

		// 汇总主控端口统计信息
		public void calcMainPortTotalInfo()
		{
			// 总分控链路数
			TotalSubControlChainNum = (SubControlChainList == null ? 0 : SubControlChainList.Count);

			// 总分控数、异常分控数、总灯具数、异常灯具数
			TotalSubControlNum = 0;
			ErrorSubControlNum = 0;
			TotalLampNum = 0;
			ErrorLampNum = 0;
			foreach (SubControlChain subControlChain in SubControlChainList)
			{
				TotalSubControlNum += subControlChain.TotalSubControlNum;
				ErrorSubControlNum += subControlChain.ErrorSubControlNum;
				TotalLampNum += subControlChain.TotalLampNum;
				ErrorLampNum += subControlChain.ErrorLampNum;
			}
		}

		// 拼接提示信息
		public string buildTipMessage()
		{
			/*
			calcMainPortTotalInfo();
			
			StringBuilder sb = new StringBuilder();
			sb.Append(" 分控数" + TotalSubControlNum);
			sb.Append(",分控异常数" + ErrorSubControlNum);
			sb.Append(",灯点总数" + TotalLampNum);
			sb.Append(",灯点异常数" + ErrorLampNum);
			
			tipMessage = sb.ToString();
			*/

			tipMessage = "";

			return tipMessage;
		}
	}
}
